using System;
using System.Diagnostics;
using System.Management.Automation;
using Nivot.PowerShell.WindowsMobile.Rapi2;

namespace Nivot.PowerShell.WindowsMobile
{
    public class WindowsMobileProviderInfo : StoreProviderInfo, IDisposable
    {
        private readonly RemoteDeviceManager _deviceManager;
        private bool m_isDisposed = false;

        public WindowsMobileProviderInfo(ProviderInfo providerInfo)
            : base(providerInfo)
        {
            Debug.WriteLine(".ctor", "WindowsMobileProviderInfo");

            _deviceManager = new RemoteDeviceManager();
        }

        public override bool ActionMatchesDerivedTypes
        {
            get
            {
                return true;
            }
        }

        internal RemoteDeviceManager DeviceManager
        {
            get
            {
                EnsureNotDisposed();
                return _deviceManager;
            }
        }

        protected bool IsDisposed
        {
            get { return m_isDisposed; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        protected void EnsureNotDisposed()
        {
            if (m_isDisposed)
            {
                throw new ObjectDisposedException("WindowsMobileProviderInfo");
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!m_isDisposed)
            {
                if (disposing)
                {
                    if (_deviceManager != null)
                    {
                        Debug.WriteLine("Dispose()", "WindowsMobileProviderInfo");
                        _deviceManager.Dispose();
                    }
                    m_isDisposed = true;
                }
            }
        }

        ~WindowsMobileProviderInfo()
        {
            Debug.WriteLine("Finalizer", "WindowsMobileProviderInfo");
            Dispose(false);
        }
    }
}